package nsu.edu.zsq.common;

import javax.servlet.http.HttpServletRequest;

import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.bean.SysUser;
import nsu.edu.zsq.bean.Teacher;

/** 
* Description: 保存每个请求的当前用户信息和HttpServletRequest<br>
*/
public class RequestHolder {
    
    /**
     * 学生账号信息
     */
    private static final ThreadLocal<Student> STUDENTHOLDER = new ThreadLocal<Student>();
    
    /**
     * 教师账号信息
     */
    private static final ThreadLocal<Teacher> TEACHERHOLDER = new ThreadLocal<Teacher>();
    
    /**
     * 管理员账号信息
     */
    private static final ThreadLocal<SysUser> SYSUSERHOLDER = new ThreadLocal<SysUser>();

    /**
     * request请求信息
     */
    private static final ThreadLocal<HttpServletRequest> REQUESTHOLDER = new ThreadLocal<HttpServletRequest>();
    
    public static void add(Student student) {
        STUDENTHOLDER.set(student);
    }
    
    public static void add(Teacher teacher) {
        TEACHERHOLDER.set(teacher);
    }
    
    public static void add(SysUser sysUser) {
        SYSUSERHOLDER.set(sysUser);
    }

    public static void add(HttpServletRequest request) {
        REQUESTHOLDER.set(request);
    }
    
    public static Student getCurrentStudent() {
        return STUDENTHOLDER.get();
    }
    
    public static Teacher getCurrentTeacher() {
        return TEACHERHOLDER.get();
    }
    public static SysUser getCurrentSysUser() {
        return SYSUSERHOLDER.get();
    }

    public static HttpServletRequest getCurrentRequest() {
        return REQUESTHOLDER.get();
    }
    
    public static void remove() {
        STUDENTHOLDER.remove();
        TEACHERHOLDER.remove();
        SYSUSERHOLDER.remove();
        REQUESTHOLDER.remove();
    }
}
